// pages/shouhou_apply/shouhou_apply.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
var regStrFn = common.regStrFn;
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  thindex:0,//1：选择退货    2：换货
	  imgarr:[],//存放图片
	  imgArrPath: [],//图片数组（从后台获取）
	  content:'',//评价内容
	},
	//输入 评价内容
	inputcontent: function (e) {
		let val = e.detail.value;
		var pos = e.detail.cursor;
		let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g
		if (!reg.test(val)) {
			this.setData({
				content: val
			})
		} else {
			let obj = regStrFn(val);
			if (pos != -1 && obj.index) {
				// /　　/计算光标的位置
				pos = obj.index.index
			}
			this.setData({
				content: obj.val,
				cursor1: pos
			})
		}
	},
	//添加图片
	addimage: function (e) {
		var that = this;
		var imgarr = this.data.imgarr;//图片数组
		var imgArrPath = this.data.imgArrPath ? this.data.imgArrPath : [];//返回的图片路径
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				const tempFilePaths = res.tempFilePaths[0];
				imgarr.push(tempFilePaths)
				that.setData({
					imgarr: imgarr
				})

				//请求图片上传接口
				wx.uploadFile({
					url: url + '/Common/uploadFile',
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						file: imgarr,
						APP_ID: APP_ID,
						sign: md5([
							"APP_ID" + APP_ID,
						])
					},
					success: function (res) {
						var data = JSON.parse(res.data);
						imgArrPath.push(data.data.src)
						that.setData({
							imgArrPath: imgArrPath,
						})
					}
				})
			}
		})
	},
	//更换图片
	changeimg: function (e) {
		var that = this;
		var imgarr = this.data.imgarr;//图片数组
		var imgArrPath = this.data.imgArrPath ? this.data.imgArrPath : [];//返回的图片路径
		var index = e.currentTarget.dataset.index;
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				const tempFilePaths = res.tempFilePaths[0];
				imgarr[index] = tempFilePaths;
				that.setData({
					imgarr: imgarr
				})

				//请求图片上传接口
				wx.uploadFile({
					url: url + '/Common/uploadFile',
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						file: imgarr,
						APP_ID: APP_ID,
						sign: md5([
							"APP_ID" + APP_ID,
						])
					},
					success: function (res) {
						var data = JSON.parse(res.data);
						// imgArrPath.push(data.data.src)
						imgArrPath[index] = data.data.src
						that.setData({
							imgArrPath: imgArrPath,
						})
					}
				})

			}
		})
	},
  //切换 退换货
	changeindex:function(e){
		var thindex = e.currentTarget.dataset.thindex;
		this.setData({
			thindex: thindex
		})
	},
	//输入 售后联系人
	inputReciever: function (e) {
		let val = e.detail.value;
		var pos = e.detail.cursor;
		let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g
		if (!reg.test(val)) {
			this.setData({
				inputName: val
			})
		} else {
			let obj = regStrFn(val);
			if (pos != -1 && obj.index) {
				// /　　/计算光标的位置
				pos = obj.index.index
			}
			this.setData({
				inputName: obj.val,
				cursor1: pos
			})
		}
	},
	//输入手机号
	inputTel: function (e) {
		this.setData({
			inputTel: e.detail.value
		})
	},
	//提交按钮
	handle_submit:function(e){
		var thindex = this.data.thindex;//2：换货  1：退货
		console.log(thindex)
		var content = this.data.content;//评论内容
		var imgArrPath = this.data.imgArrPath;//要传给后台的图片数据 最少一张
		var inputName = this.data.inputName;//联系人
		var inputTel = this.data.inputTel;//联系人电话

		if (!thindex){
			wx.showToast({
				title: '请选择售后类型',
				icon: 'none',
				mask: true,
			})
			return false;
		}
		if (!content) {
			wx.showToast({
				title: '请输入问题描述',
				icon: 'none',
				mask: true,
			})
			return false;
		}
		if (imgArrPath.length<1) {
			wx.showToast({
				title: '请添加至少一张图片',
				icon: 'none',
				mask: true,
			})
			return false;
		}
		if (!inputName) {
			wx.showToast({
				title: '请输入联系人姓名',
				icon: 'none',
				mask: true,
			})
			return false;
		}

		if (inputName.length < 2) {
			wx.showToast({
				title: '联系人姓名不得少于2个字',
				icon: 'none',
				mask: true,
			})
			return false;
		}

		if (!inputTel) {
			wx.showToast({
				title: '请输入联系人电话',
				icon: 'none',
				mask: true,
			})
			return false;
		}
		var session_str = wx.getStorageSync('session_str');//获取密钥
		if (!session_str) {
			wx.navigateTo({
				url: '../author/author',
			})
			return false;
		}
		var order_id = this.data.id;//订单id
		//请求申请售后的接口
		wx.request({
			url: url + 'After/applyAfter',
			data: {
				order_id: order_id,
				type: thindex,
				remark: content,
				name: inputName,
				telephone: inputTel,
				imgs: imgArrPath,
				session_str: session_str,
				APP_ID: APP_ID,
				sign: md5([
					"order_id" + order_id,
					"type" + thindex,
					"remark" + content,
					"name" + inputName,
					"telephone" + inputTel,
					"session_str" + session_str,
					"APP_ID" + APP_ID
				])
			},
			header: {},
			method: 'post',
			dataType: 'json',
			responseType: 'text',
			success: function (res) {
				wx.hideLoading();
				if (res.data.code == 200) {
					wx.showToast({
						title: '操作成功',
						icon: 'success',
						mask: true,
					})
					setTimeout(function(){
						wx.navigateBack({
							delta: 2,
						})
					},1500)
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						mask: true,
					})
				}
			},
			fail: function (res) {
				wx.hideLoading();
				wx.showToast({
					title: '网络错误',
					icon: 'none',
					mask: true,
				})
			}
		});


	},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  var id = options.id;
	  this.setData({
		  id: id
	  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	  var that = this;
	  var id = this.data.id;//订单id
	  var session_str = wx.getStorageSync('session_str');//获取密钥
	  wx.request({
		  url: url + 'Order/orderInfo',
		  data: {
			  session_str: session_str,
			  order_id: id,
			  APP_ID: APP_ID,
			  sign: md5([
				  "session_str" + session_str,
				  "order_id" + id,
				  "APP_ID" + APP_ID,
			  ])
		  },
		  header: {},
		  method: 'post',
		  dataType: 'json',
		  responseType: 'text',
		  success: function (res) {
			  wx.hideLoading();
			  if (res.data.code == 200) {
				  that.setData({
					  orderinfo: res.data.data,//订单数据
				  })
			  } else {
				  wx.showToast({
					  title: res.data.msg,
					  icon: 'none',
					  mask: true,
				  })
			  }
		  },
		  fail: function (res) {
			  wx.hideLoading();
			  wx.showToast({
				  title: '网络错误',
				  icon: 'none',
				  mask: true,
			  })
		  }
	  });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})